#include<iostream>
#include<cstring>
using namespace std;
const int N = 3e5 + 10, M = N * 2;
int h[N], e[M], ne[M], idx;
int dep[N], fa[N], f[N][20];
int d[N];
int n;
int w[N];
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs(int u, int father)
{
	fa[u] = father;
	dep[u] = dep[father] + 1;
	f[u][0] = father;
	for (int i = 1; 1 << i <= dep[u]; i++) {
		f[u][i] = f[f[u][i - 1]][i - 1];
	}
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (j == father)continue;
		dfs(j, u);
	}
}

int lca(int u, int v)
{
	if (dep[u] < dep[v])swap(u, v);
	int d = dep[u] - dep[v];
	for (int i = 0; d; i++) {
		if (d & 1) {
			u = f[u][i];
		}
		d /= 2;
	}
	if (u == v)return u;
	for (int i = 19; i >= 0; i--) {
		if (f[u][i] != f[v][i]) {
			u = f[u][i];
			v = f[v][i];
		}
	}
	return f[u][0];
}

void dfs2(int u, int father)
{
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (j == father)continue;
		dfs2(j, u);
		d[u] += d[j];
	}
}
int main()
{
	cin >> n;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n; i++) {
		scanf_s("%d", &w[i]);
	}
	for (int i = 0; i < n - 1; i++) {
		int a, b;
		scanf_s("%d%d", &a, &b);
		add(a, b);
		add(b, a);
	}
	dfs(1, 0);
	for (int i = 2; i <= n; i++) {
		int u = w[i - 1];
		int v = w[i];
		//cout << "lca==" << lca(u, v) << endl;
		d[u]++, d[v]++, d[lca(u, v)]--, d[fa[lca(u, v)]]--;
	}
	dfs2(1, 0);
	//cout << "d[1]===" << d[1] << endl;
	for (int i = 2; i <= n; i++) {
		d[w[i]]--;
	}
	for (int i = 1; i <= n; i++) {
		printf("%d\n", d[i]);
	}
	return 0;
}